<?php
	class studenthalloffamemodel extends Model {
		function studenthalloffamemodel()
		{
			parent::Model();			
		}
		
		function add($data)
		{
            $this->db->set('is_active', '1', FALSE);
            $this->db->set('create_by', "'" . $this->session->userdata('username') ."'", FALSE);
            $this->db->set('create_date', 'NOW()', FALSE); 
			$this->db->insert('stu_hall_of_fame', $data);
			return $this->db->insert_id();
		}
		
		function updateImagePath($d)
		{			
			$data = array(
               'image' => $d['image']			   
            );
            $this->db->where('stu_id', $d['stu_id']);
			$this->db->update('stu_hall_of_fame', $data); 
		}
		
		function update($data)
		{			
			$dataDB = array(
			   'name' => $data['name'] ,
				'nick_name' => $data['nick_name'] ,
				'school_name' => $data['school_name'] ,
			'current_class' => $data['current_class'] ,
			'tutor_name' => $data['tutor_name'] ,
               'detail' => $data['detail'] ,
			   'show' => $data['show'] ,
			   'update_by' => $this->session->userdata('username')
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('stu_id', $data['stu_id']);
			$this->db->update('stu_hall_of_fame', $dataDB); 
		}
		
		function getTop()
		{
			$this->db->order_by("stu_id", "asc"); 
			$query = $this->db->get_where('stu_hall_of_fame', array('is_active' => 1, 'show' => 'Y'));
			return $query->result();
		}
		
	function getAllLimit($limit = NULL, $offset = NULL)
		{
			$sql = "select s.name, s.nick_name, s.school_name, s.current_class, s.tutor_name, s.detail
						from stu_hall_of_fame s
						where is_active = 1 and name is not null
						order by name limit ";
			if($offset != null) {
				$sql .= $offset . ", ";
			}
			$sql .= $limit;
			$this->db->limit($limit, $offset);	
			$query = $this->db->query($sql);		
			return $query->result();
		}
		
		function getAllLimit2($limit = NULL, $offset = NULL)
		{
//			$this->output->enable_profiler(TRUE);
			//$query = $this->db->get('mytable', 10, 20);

// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
			
			$sql = "select s.name, s.nick_name, s.school_name, s.current_class, s.tutor_name, s.detail, o.seq
						from stu_hall_of_fame s, school_order o
						where is_active = 1 and name is not null
								and s.school_name in (select o.school_name from school_order)
						union
							select s.name, s.nick_name, s.school_name, s.current_class, s.tutor_name, s.detail, '9999' as seq
							from stu_hall_of_fame s 
							where is_active = 1 and name is not null
								and s.school_name not in (select school_name from school_order)
						order by seq, name limit ";
			if($offset != null) {
				$sql .= $offset . ", ";
			}
			$sql .= $limit;
			$this->db->limit($limit, $offset);	
			$query = $this->db->query($sql);		
			return $query->result();
		}
		
		function getAll()
		{
			$this->db->order_by("stu_id", "asc"); 
			$query = $this->db->get_where('stu_hall_of_fame', array('is_active' => 1));
			return $query->result();
		}
		
		function getByID($user)
		{
			$query = $this->db->get_where('stu_hall_of_fame', array('stu_id' => $user, 'is_active' => 1));
			return $query->row_array();
		}
		
		function delete($newsID)
		{
			$data = array(
               'is_active' => 0,
			   'update_by' => $this->session->userdata('username')   
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('stu_id', $newsID);
			$this->db->update('stu_hall_of_fame', $data); 
		}
		
		function count_all()
	 	{
	 		$this->db->where('name IS NOT NULL', null, false);
	 		$query = $this->db->getwhere('stu_hall_of_fame', array('is_active'=>1));
			$count = $query->num_rows(); 
	  		return $count;
	 	}
	}
?>